home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtolj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  3.8 KB  |  172 lines

  1. /* pbmtolj.c - read a portable bitmap and produce a LaserJet bitmap file
  2. **    
  3. **    based on pbmtops.c
  4. **
  5. **    Michael Haberler HP Vienna mah@hpuviea.uucp
  6. **                   mcvax!tuvie!mah
  7. **    misfeatures: 
  8. **        no positioning
  9. **
  10. **      Bug fix Dec 12, 1988 :
  11. **              lines in putbit() reshuffled 
  12. **              now runs OK on HP-UX 6.0 with X10R4 and HP Laserjet II
  13. **      Bo Thide', Swedish Institute of Space Physics, Uppsala <bt@irfu.se>
  14. **
  15. ** Copyright (C) 1988 by Jef Poskanzer and Michael Haberler.
  16. **
  17. ** Permission to use, copy, modify, and distribute this software and its
  18. ** documentation for any purpose and without fee is hereby granted, provided
  19. ** that the above copyright notice appear in all copies and that both that
  20. ** copyright notice and this permission notice appear in supporting
  21. ** documentation.  This software is provided "as is" without express or
  22. ** implied warranty.
  23. */
  24.  
  25. #include "pbm.h"
  26.  
  27. static int dpi = 75;
  28.  
  29. static void putinit ARGS(( void ));
  30. static void putbit ARGS(( bit b ));
  31. static void putrest ARGS(( void ));
  32. static void putitem ARGS(( void ));
  33.  
  34. void
  35. main( argc, argv )
  36.     int argc;
  37.     char* argv[];
  38.     {
  39.     FILE* ifp;
  40.     bit* bitrow;
  41.     register bit* bP;
  42.     int argn, rows, cols, format, rucols, padright, row;
  43.     register int nzcol, col;
  44.     char* usage = "[-resolution N] [pbmfile]\n\tresolution = [75|100|150|300] (dpi)";
  45.  
  46.     pbm_init( &argc, argv );
  47.  
  48.     argn = 1;
  49.  
  50.     /* Check for flags. */
  51.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  52.     {
  53.     if ( pm_keymatch( argv[argn], "-resolution", 2 ) )
  54.         {
  55.         ++argn;
  56.         if ( argn == argc || sscanf( argv[argn], "%d", &dpi ) != 1 )
  57.         pm_usage( usage );
  58.         }
  59.     else
  60.         pm_usage( usage );
  61.     ++argn;
  62.     }
  63.  
  64.     if ( argn != argc )
  65.     {
  66.     ifp = pm_openr( argv[argn] );
  67.     ++argn;
  68.     }
  69.     else
  70.     ifp = stdin;
  71.  
  72.     if ( argn != argc )
  73.     pm_usage( usage );
  74.  
  75.     pbm_readpbminit( ifp, &cols, &rows, &format );
  76.     bitrow = pbm_allocrow( cols );
  77.  
  78.     putinit( );
  79.     for ( row = 0; row < rows; ++row )
  80.     {
  81.     pbm_readpbmrow( ifp, bitrow, cols, format );
  82.  
  83.     /* Find rightmost black pixel. */
  84.     for ( nzcol = cols - 1; nzcol >= 0 && bitrow[nzcol] == PBM_WHITE; --nzcol )
  85.         continue;
  86.  
  87.     /* Round up to the nearest multiple of 8. */
  88.     rucols = ( nzcol + 8 ) / 8;
  89.     rucols = rucols * 8;
  90.     padright = rucols - (nzcol + 1);
  91.  
  92.     /* Transfer raster graphics */
  93.      fprintf (stdout, "\033*b%dW",rucols/8);
  94.         for ( col = 0, bP = bitrow; col <= nzcol; ++col, ++bP )
  95.         putbit( *bP );
  96.     for ( col = 0; col < padright; ++col )
  97.         putbit( 0 );
  98.         }
  99.  
  100.     pm_close( ifp );
  101.  
  102.     putrest( );
  103.  
  104.     pm_close (stdout);
  105.  
  106.     exit( 0 );
  107.     }
  108.  
  109. static int item, bitsperitem, bitshift, itemsperline, firstitem;
  110.  
  111. static void
  112. putinit( )
  113.     {
  114.     /* Printer reset. */
  115.     fprintf (stdout, "\033E");
  116.  
  117.     /* Ensure top margin is zero */
  118.     fprintf (stdout, "\033&l0E");
  119.  
  120.     /* Set raster graphics resolution */
  121.     fprintf (stdout, "\033*t%dR",dpi);
  122.  
  123.     /* Start raster graphics, relative adressing */
  124.     fprintf (stdout, "\033*r1A");
  125.  
  126.     itemsperline = 0;
  127.     bitsperitem = 1;
  128.     item = 0;
  129.     bitshift = 7;
  130.     firstitem = 1;
  131.     }
  132.  
  133. #if __STDC__
  134. static void
  135. putbit( bit b )
  136. #else /*__STDC__*/
  137. static void
  138. putbit( b )
  139. bit b;
  140. #endif /*__STDC__*/
  141.     {
  142.     if ( b == PBM_BLACK )
  143.     item += 1 << bitshift;
  144.     bitshift--;
  145.     if ( bitsperitem == 8 ) {
  146.     putitem( );
  147.         bitshift = 7;
  148.     }
  149.     bitsperitem++;
  150.     }
  151.  
  152. static void
  153. putrest( )
  154.     {
  155.     if ( bitsperitem > 1 )
  156.     putitem( );
  157.  
  158.     /* end raster graphics */
  159.     fprintf (stdout,  "\033*rB" );
  160.  
  161.     /* Printer reset. */
  162.     fprintf (stdout, "\033E");
  163.     }
  164.  
  165. static void
  166. putitem( )
  167.     {
  168.     putchar( item );
  169.     bitsperitem = 0;
  170.     item = 0;
  171.     }
  172.